package com.game.sdk.login;

import java.io.InputStream;

import android.text.TextUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import com.game.sdk.YTAppService;
import com.game.sdk.domain.ParamJson;
import com.game.sdk.domain.ResultCode;
import com.game.sdk.init.NetCallBack;
import com.game.sdk.util.DataSafeUtil;
import com.game.sdk.util.DialogUtil;
import com.game.sdk.util.GetDataImpl;
import com.game.sdk.util.NetTask;
import com.game.sdk.util.Util;

/**
 * 登陆的一的一些工具
 * 
 * @日志 在之后的的时间，登录的所有借口都要放在这里
 * @日志 getparamsObs()系列参数，生成paramskey是最后一个设置 否则服务器返回参数校验不通过
 * 
 * @author ling
 * */
public class LoginUtils {

	/**
	 * 退出登录
	 * 
	 * @param mem_id
	 *            相当于username
	 * @param user_token
	 *            相当于密码
	 * @param ctx
	 *            上下文
	 * @param callback
	 *            请求回调
	 * @NetTask 不懂的地方去查看NetTask
	 * @return void
	 * @author ling
	 */
	public static void loginOut(String mem_id, String user_token,
			final Context ctx, final NetCallBack callback) {
		String jasonStr = "";
		jasonStr = getLoginOutParamsObjs(mem_id, user_token).toString();
		new NetTask() {

			protected void onPostExecute(String result) {
				Util.netResponseCommHandler(1, result, callback);
			};
		}.execute(ctx, Constant.URL_LOGIN_OUT, jasonStr, true, true, true, true);
	}

	/**
	 * 退出登录的参数获取
	 * 
	 * @param mem_id
	 *            相当于username
	 * @param user_token
	 *            相当于密码
	 * @return void
	 * */
	private static Object getLoginOutParamsObjs(String mem_id, String user_token) {
		ParamJson json = new ParamJson();
		json.setAppid(YTAppService.appid);

		json.setImei(YTAppService.dm.imeil);

		json.setFrom(ParamJson.ANDROID + "");// android 就用1

		json.setAgentgame(YTAppService.agentid);

		json.setDeviceinfo(YTAppService.dm.deviceinfo);

		json.setUserua(YTAppService.dm.userua);

		json.setUserid(mem_id);

		json.setUser_token(user_token);

		json.setCode(DataSafeUtil.code + "");

		json.setClient_id(YTAppService.clientId + "");

		String api_token = DataSafeUtil.getApiToken("logout",
				System.currentTimeMillis(), YTAppService.clientKey);
		json.setApi_token(api_token);

		json.setIpaddrid(YTAppService.city_id);
		
		json.setParams_key(DataSafeUtil.getIdentifyKey(json.getParamsString(),
				YTAppService.clientKey));

		return json.buildParams();

	}

	/**
	 * 登录
	 * 
	 * @param username
	 *            用户名
	 * @param pwd
	 *            密码
	 * @param ctx
	 *            上下文
	 * @param callback
	 *            请求回调
	 * @return void
	 */
	public static void login(String username, String pwd, final Context ctx,
			final NetCallBack callback) {
		String jasonStr = "";
		jasonStr = getLoginParamsObjs(username, pwd).toString();
		new NetTask() {

			protected void onPostExecute(String result) {
				Util.netResponseCommHandler(1, result, callback);
			};
		}.execute(ctx, Constant.URL_LOGIN_NORMAL, jasonStr, true, true, true,
				true);
	}

	/**
	 * 普通登录参数获取
	 * 
	 * @param username
	 *            用户名
	 * 
	 * @param pwd
	 *            密码
	 * */
	private static Object getLoginParamsObjs(String username, String pwd) {
		ParamJson json = new ParamJson();
		json.setAppid(YTAppService.appid);

		json.setUsername(username);

		json.setImei(YTAppService.dm.imeil);

		json.setFrom(ParamJson.ANDROID + "");// android 就用1

		json.setAgentgame(YTAppService.agentid);

		json.setDeviceinfo(YTAppService.dm.deviceinfo);

		json.setUserua(YTAppService.dm.userua);

		json.setPassword(pwd);

		json.setCode(DataSafeUtil.code + "");

		json.setClient_id(YTAppService.clientId + "");

		String api_token = DataSafeUtil.getApiToken("login",
				System.currentTimeMillis(), YTAppService.clientKey);
		json.setApi_token(api_token);

		json.setIpaddrid(YTAppService.city_id);

		json.setParams_key(DataSafeUtil.getIdentifyKey(json.getParamsString(),
				YTAppService.clientKey));
		
		return json.buildParams();

	}

	/**
	 * 注册
	 * 
	 * @param username
	 *            用户名
	 * @param pwd
	 *            密码
	 * @param ctx
	 *            上下文
	 * @param callback
	 *            请求回调
	 * @return void
	 */
	public static void regist(final Context ctx, String username, String pwd,
			final NetCallBack callback) {
		String jasonStr = "";
		jasonStr = getRegistParamsObjs(username, pwd).toString();
		new NetTask() {
			protected void onPostExecute(String result) {
				Util.netResponseCommHandler(1, result, callback);
			};
		}.execute(ctx, Constant.REGIST_NORMAL_URL, jasonStr, true, true, true,
				true);
	}

	/**
	 * 普通注册参数 * @param username 用户名
	 * 
	 * @param pwd
	 *            密码
	 * @return Object
	 * */
	private static Object getRegistParamsObjs(String username, String password) {
		ParamJson json = new ParamJson();

		json.setAppid(YTAppService.appid);

		json.setImei(YTAppService.dm.imeil);

		json.setUsername(username);

		json.setFrom(ParamJson.ANDROID + "");// android 就用1

		json.setAgentgame(YTAppService.agentid);

		json.setUserua(YTAppService.dm.userua);

		json.setDeviceinfo(YTAppService.dm.deviceinfo);

		json.setCode(DataSafeUtil.code + "");

		json.setPassword(password);

		json.setClient_id(YTAppService.clientId + "");

		String api_token = DataSafeUtil.getApiToken("registerNew",
				System.currentTimeMillis(), YTAppService.clientKey);
		json.setApi_token(api_token);

		json.setIpaddrid(YTAppService.city_id);

		json.setParams_key(DataSafeUtil.getIdentifyKey(json.getParamsString(),
				YTAppService.clientKey));

		return json.buildParams();

	}

	/**
	 * 一键注册
	 * 
	 * @param ctx
	 *            上下文
	 * @param callback
	 *            注册的接口回调
	 * @return
	 */
	public static void registOne(final Context ctx, final NetCallBack callback) {
		String jasonStr = "";
		jasonStr = getRegistOneParamsObjs().toString();
		new NetTask() {

			protected void onPostExecute(String result) {
				Util.netResponseCommHandler(1, result, callback);
			};
		}.execute(ctx, Constant.REGIST_ONE_URL, jasonStr, true, true, true,
				true);
	}

	/**
	 * 获取一键注册的参数
	 * 
	 * @return Object 写成Object是为了能够很好的兼容各个接口
	 * */
	private static Object getRegistOneParamsObjs() {
		ParamJson json = new ParamJson();
		json.setAppid(YTAppService.appid);

		json.setImei(YTAppService.dm.imeil);

		json.setFrom(ParamJson.ANDROID + "");// android 就用1

		json.setDeviceinfo(YTAppService.dm.deviceinfo);

		json.setUserua(YTAppService.dm.userua);

		json.setAgentgame(YTAppService.agentid);

		json.setCode(DataSafeUtil.code + "");

		json.setClient_id(YTAppService.clientId + "");

		String api_token = DataSafeUtil.getApiToken("registerOne",
				System.currentTimeMillis(), YTAppService.clientKey);
		json.setApi_token(api_token);

		json.setIpaddrid(YTAppService.city_id);

		json.setParams_key(DataSafeUtil.getIdentifyKey(json.getParamsString(),
				YTAppService.clientKey));

		return json.buildParams();

	}

	/**
	 * 短信注册包括获取验证码注册
	 * 
	 * @param username
	 *            在短信注册的时候，我们需要一个用户名。也就是手机号码
	 * @param pwd
	 *            密码
	 * @param smsCode
	 *            收到的短信验证码
	 * @param issend
	 *            标识是否已经发送过验证码 一般都是1
	 * @param ctx
	 *            上下文
	 * @param callback
	 *            短信注册的接口回调
	 * @return
	 */
	public static void registMsg(String username, String pwd, String smsCode,
			String issend, final Context ctx, final NetCallBack callback) {
		String jasonStr = "";
		jasonStr = getRegistMsgParamsObjs(username, pwd, smsCode, issend)
				.toString();
		new NetTask() {

			protected void onPostExecute(String result) {
				Util.netResponseCommHandler(1, result, callback);
			};
		}.execute(ctx, Constant.URL_REGIST_MSG, jasonStr, true, true, true,
				true);
	}

	/**
	 * 短信注册包括获取验证码注册
	 * */
	private static Object getLoginMsgParamsObjs(String username,
			String smsCode, String issend) {
		ParamJson json = new ParamJson();
		json.setAppid(YTAppService.appid);

		json.setUsername(username);

		json.setImei(YTAppService.dm.imeil);

		json.setFrom(ParamJson.ANDROID + "");// android 就用1

		json.setAgentgame(YTAppService.agentid);

		json.setDeviceinfo(YTAppService.dm.deviceinfo);

		json.setUserua(YTAppService.dm.userua);

		json.setIssend(issend);
		if ("1".equals(issend)) {
			json.setSendcode(smsCode);// regist
		}

		json.setUserphone(username);

		json.setCode(DataSafeUtil.code + "");

		json.setClient_id(YTAppService.clientId + "");

		String api_token = DataSafeUtil.getApiToken("loginMobile",
				System.currentTimeMillis(), YTAppService.clientKey);
		json.setApi_token(api_token);

		json.setIpaddrid(YTAppService.city_id);

		json.setParams_key(DataSafeUtil.getIdentifyKey(json.getParamsString(),
				YTAppService.clientKey));
		
		return json.buildParams();

	}

	/**
	 * 短信登录包括获取验证码和登录
	 * 
	 * @param jasonStr
	 * @return
	 */
	public static void loginMsg(String username, String smsCode, String issend,
			final Context ctx, final NetCallBack callback) {
		String jasonStr = "";
		jasonStr = getLoginMsgParamsObjs(username, smsCode, issend).toString();
		new NetTask() {

			protected void onPostExecute(String result) {
				Util.netResponseCommHandler(1, result, callback);
			};
		}.execute(ctx, Constant.URL_LOGIN_MSG, jasonStr, true, true, true, true);
	}

	/**
	 * 短信注册包括获取验证码注册
	 * */
	private static Object getRegistMsgParamsObjs(String username, String pwd,
			String smsCode, String issend) {
		ParamJson json = new ParamJson();
		json.setAppid(YTAppService.appid);

		json.setUsername(username);

		json.setImei(YTAppService.dm.imeil);

		json.setFrom(ParamJson.ANDROID + "");// android 就用1

		json.setAgentgame(YTAppService.agentid);

		json.setDeviceinfo(YTAppService.dm.deviceinfo);

		json.setUserua(YTAppService.dm.userua);

		json.setIssend(issend);
		if ("1".equals(issend)) {
			json.setPassword(pwd);// regist
			json.setSendcode(smsCode);// regist
		}

		json.setUserphone(username);

		json.setCode(DataSafeUtil.code + "");

		json.setClient_id(YTAppService.clientId + "");

		String api_token = DataSafeUtil.getApiToken("registerMobile",
				System.currentTimeMillis(), YTAppService.clientKey);
		json.setApi_token(api_token);

		json.setIpaddrid(YTAppService.city_id);

		json.setParams_key(DataSafeUtil.getIdentifyKey(json.getParamsString(),
				YTAppService.clientKey));

		return json.buildParams();

	}

}
